C 的 printf 函数将值打印到终端,而 scanf 函数读取用户输入的值。 printf 和 scanf 函数属于 C 的标准 I/O 库,需要通过使用 #include <stdio.h>
显式包含在使用这些函数的任何 .c 文件的顶部。在本节中,我们介绍在 C 程序中使用 printf 和 scanf 的基础知识。第 2 章的“I/O”部分更详细地讨论了 C 的输入和输出函数。
1.2.1 prinf
C 的 printf 函数与 Python 中的格式化打印非常相似,调用者指定要打印的格式字符串。格式字符串通常包含格式说明符,例如打印制表符 (\t) 或换行符 (\n) 等特殊字符,或输出中值的占位符。占位符由 % 后面接着类型说明符的字母(例如,%d 表示整数值的占位符)。对于格式字符串中的每个占位符,printf 需要一个附加参数。表 1 包含一个使用 Python 和 C 语言编写的示例程序,并带有格式化输出:
# Python formatted print example
def main():
print("Name: %s, Info:" % "Vijay")
print("\tAge: %d \t Ht: %g" %(20,5.9))
print("\tYear: %d \t Dorm: %s" %(3, "Alice Paul"))
# call the main function:
main()
/* C printf example */
#include <stdio.h> // needed for printf
int main(void) {
printf("Name: %s, Info:\n", "Vijay");
printf("\tAge: %d \t Ht: %g\n",20,5.9);
printf("\tYear: %d \t Dorm: %s\n", 3,"Alice Paul");
return 0;
}
运行时,该程序的两个版本都会产生相同格式的输出:
Name: Vijay, Info:
Age: 20 Ht: 5.9
Year: 3 Dorm: Alice Paul
C 的 printf 函数 和 Python 的 print 函数之间的主要区别在于,Python 在输出字符串的末尾隐式包含打印换行符,但 C 版本则不然。因此,本示例中的 C 格式字符串末尾有换行符 (\n),以显式打印换行符。在 C 的 printf 函数 和 Python 的 print 函数中,列出格式字符串中占位符的参数值的语法也略有不同。
C 使用与 Python 相同的格式化占位符来指定不同类型的值。前面的示例演示了以下格式化占位符:
%g: placeholder for a float (or double) value
%d: placeholder for a decimal value (int, short, char)
%s: placeholder for a string value
C 另外支持 %c 占位符来打印字符值。当程序员想要打印与特定数字编码关联的 ASCII 字符时,此占位符非常有用。以下是C 代码示例,它将 char 打印为其数值 (%d) 及其字符编码 (%c):
// Example printing a char value as its decimal representation (%d)
// and as the ASCII character that its value encodes (%c)
char ch;
ch = 'A';
printf("ch value is %d which is the ASCII value of %c\n", ch, ch);
ch = 99;
printf("ch value is %d which is the ASCII value of %c\n", ch, ch);
运行时,程序的输出如下所示:
ch value is 65 which is the ASCII value of A
ch value is 99 which is the ASCII value of c
1.2.2 scanf
C 的 scanf 函数代表一种读取用户输入的值(通过键盘)并将其存储在程序变量中的方法。 scanf函数对用户输入数据的确切格式可能有点挑剔,这意味着它对格式不好的用户输入不是很健壮。在第2章的“I/O”部分,我们讨论了从用户读取输入值的更健壮的方法。请记住,如果您的程序由于格式错误的用户输入而进入无限循环,您总是可以按CTRL-C来终止它。
Python 和 C 中读取输入的处理方式不同:Python 使用输入函数将值作为字符串读入,然后程序将字符串值转换为 int,而 C 使用 scanf 读入 int 值并将其存储位于 int 程序变量的内存位置(例如,&num1)。表 2 显示了用 Python 和 C 语言读取用户输入值的示例程序:
# Python input example
def main():
num1 = input("Enter a number:")
num1 = int(num1)
num2 = input("Enter another:")
num2 = int(num2)
print("%d + %d = %d" % (num1, num2, (num1+num2)))
# call the main function:
main()
/* C input (scanf) example */
#include <stdio.h>
int main(void) {
int num1, num2;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another: ");
scanf("%d", &num2);
printf("%d + %d = %d\n", num1, num2, (num1+num2));
return 0;
}
运行时,两个程序都会读入两个值(此处为 30 和 67):
Enter a number: 30
Enter another: 67
30 + 67 = 97
与 printf 一样,scanf 采用格式字化符串来指定要读入的值的数量和类型(例如,“%d”指定一个 int 值)。 scanf 函数在读取数值时会去除前后空格,因此其格式化字符串只需要包含一系列格式化占位符,通常在其格式化字符串中的占位符之间没有空格或其他格式化字符。格式字符串中占位符的参数指定将存储读入的值的程序变量的位置。在变量名称前加上 & 运算符会生成该变量在程序内存中的位置 ——变量的内存地址。第 2 章中的“指针”部分更详细地讨论了 & 运算符。目前,我们仅在 scanf 函数的上下文中使用它。
这是另一个 scanf 示例,其中格式化字符串有两个值的占位符,第一个是 int,第二个是 float:
int x;
float pi;
// read in an int value followed by a float value ("%d%g")
// store the int value at the memory location of x (&x)
// store the float value at the memory location of pi (&pi)
scanf("%d%g", &x, &pi);
通过 scanf 将数据输入到程序时,各个数字输入值必须用至少一个空格字符分隔。但是,由于 scanf 会跳过额外的前后空白字符(例如空格、制表符和换行符),因此用户可以在每个输入值之前或之后输入任意数量的空格。例如,如果用户在前面的示例中输入以下内容来调用 scanf,则 scanf 将读取 8 并将其存储在 x 变量中,然后读取 3.14 并将其存储在 pi 变量中:
8 3.14